﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShipMents_Add.aspx.cs" Inherits="Automobile.service.UI.ShipMents_Add" %>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,member-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<script type="text/javascript" src="lib/PIE_IE678.js"></script>
<![endif]-->
    <link href="css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="css/H-ui.admin.css" rel="stylesheet" type="text/css" />
    <link href="lib/Hui-iconfont/1.0.1/iconfont.css" rel="stylesheet" type="text/css" />
    <!--[if IE 6]>
<script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
    <title>零售</title>
</head>
<body>

    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 用户中心 <span class="c-gray en">&gt;</span> 零售 <a class="btn btn-success radius r mr-20" style="line-height: 1.6em; margin-top: 3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
    <div class="pd-20">
        <form runat="server">
                   <span>单据号：</span><asp:TextBox ID="Shipments_blitems" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   <span>业务员：</span><asp:TextBox ID="Shipments_Per" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   <span>单据说明：</span><asp:TextBox ID="Shipments_Por" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   <span>操作员：</span><asp:TextBox ID="Shipments_Peosor" runat="server"></asp:TextBox><br />
                   <span>会员编号：</span><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                   <span>仓库：</span><asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><br />
        </form>
    </div>
    <input type="text" class="input-text" style="width: 250px" placeholder="请输入商品编码" id="ids">
    <button type="submit" class="btn btn-success radius" onclick="fun()"><i class="Hui-iconfont">&#xe665;</i> 搜商品</button>
    <div class="mt-20">
        <table class="table table-border table-bordered table-hover table-bg table-sort">
            <thead>
                <tr class="text-c">
                    <th width="80">商品编号</th>
                    <th width="80">商品名称</th>
                    <th width="40">商品规格</th>
                    <th width="90">商品进价</th>
                    <th width="100">商品编码</th>
                    <th width="100">商品备注</th>
                    <th width="70">购买数量</th>
                    <th width="70">总金额</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <a class="btn btn-primary radius" onclick="Sum_ShipMentsPrice()">添加销售单据</a>
    </div>
    <div class="pd-20" id="sumprice">
        <div class="row cl">
            <label class="form-label col-3"><span class="c-red">*</span>应收金额：</label>
            <div class="formControls col-5">
                ￥<asp:Label ID="Shipments_Price" runat="server" Text=""></asp:Label>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-3"><span class="c-red">*</span>实收金额：</label>
            <div class="formControls col-5">
                <input type="text" class="input-text" runat="server" id="Shipments_Pricepor" datatype="*2-16" nullmsg="实收金额不能为空">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-3"><span class="c-red">*</span>备注：</label>
            <div class="formControls col-5">
                <textarea id="Shipments_Remark" name="Shipments_Remark" cols="100" rows="5"></textarea>
            </div>
        </div>
        <div class="row cl">
            <div class="col-9 col-offset-3">
                <a class="btn btn-primary radius" onclick="AddShipMents()">确定</a>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="lib/layer/1.9.3/layer.js"></script>
    <script type="text/javascript" src="lib/laypage/1.2/laypage.js"></script>
    <script type="text/javascript" src="lib/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="js/H-ui.js"></script>
    <script type="text/javascript" src="js/H-ui.admin.js"></script>
    <script type="text/javascript">
        function fun() {
            var id = $("#ids").val();
            if ($("#num" + id + "").val() != null) {
                alert("已添加此商品");
                return false;
            }

            $.ajax({
                type: "post",
                url: "Hander/GetProductByID.ashx",
                data: {
                    id: $("#ids").val()
                },
                success: function (data) {
                    var data1 = eval(data);
                    var str = "<tr class='text-c'><td><span name='Product_Id'>" + data1[0].Product_Id + "</span></td><td>" + data1[0].Product_Name + "</td><td>" + data1[0].Product_Modelspe + "</td><td><span name='Product_Bprid' id='Price" + data1[0].Product_Id + "' >" + data1[0].Product_Bprid + "</span>元</td><td>" + data1[0].Product_Barcode + "</td><td>" + data1[0].Product_Remark + "</td><td><input type='text' name='Sum' style='width:50px' onchange='Sum(" + data1[0].Product_Id + ")' id='num" + data1[0].Product_Id + "'/></td><td><span name='SumMoney' id='sum" + data1[0].Product_Id + "'>0</span>元</td></tr>";
                    $(".table-border tbody").append(str);
                }
            })

        }
        //小计
        function Sum(id) {
            var num = $("#num" + id + "").val()
            var tt = /^\d+$/g;
            if (!tt.test(num)) {
                alert('必须是正整数');
                return false;
            }
            var price = $("#Price" + id + "").text();
            $("#sum" + id + "").text(price * num);
        }


        function Sum_ShipMentsPrice() {
            var sumshipprice = 0;
            var c = document.getElementsByName("SumMoney");
            for (var i = 0; i < c.length; i++) {
                sumshipprice += parseFloat(c[i].innerHTML);
            }
            document.getElementById('Shipments_Price').innerHTML = sumshipprice;
            $("#Shipments_Pricepor").val(sumshipprice);
        }
        function AddShipMents()
        {
            if ($("#DropDownList1").val() == "-请选择-" || $("#DropDownList2").val() == "-请选择-") {
                alert("请选择供货商和仓库");
                return false;
            }
            //获取商品编号
            var d = document.getElementsByName("Product_Id");
            var Product_Id = new Array();
            for (var i = 0; i < d.length; i++) {
                Product_Id[i] = d[i].innerHTML;
            }

            //获取商品单价
            var a = document.getElementsByName("Product_Bprid");
            var Product_Bprid = new Array();
            for (var i = 0; i < a.length; i++) {
                Product_Bprid[i] = a[i].innerHTML;
            }
            //获取各个商品的购买数量
            var Sum = new Array();
            var i = -1;
            $("[name='Sum']").each(function () {
                i++;
                Sum[i] = $(this).val();
            })
            //获去各个商品的商品小计
            var sumshipprice = 0;
            var c = document.getElementsByName("SumMoney");
            var SumMoney = new Array();
            for (var i = 0; i < c.length; i++) {
                SumMoney[i] = c[i].innerHTML;
                sumshipprice += parseFloat(c[i].innerHTML);
            }
            if (parseFloat(document.getElementById('Shipments_Price').innerHTML)!=sumshipprice)
            {
                alert('应付金额错误');
            }
            else
            {
                //字符拼接xml
                var xml = "<xml>";
                for (var i = 0; i < Sum.length; i++) {
                    xml += "<product Product_Id='" + Product_Id[i] + "' ShipmentMate_Num='" + Sum[i] + "' ShipmentMate_Price='" + Product_Bprid[i] + "' ShipmentMate_Sumprice='" + SumMoney[i] + "'/>";
                }
                xml += "</xml>";
                var encodexml = encodeURIComponent(xml);// 对xml进行编码

                $.ajax({
                    type: "post",
                    url: "Hander/AddShipMents.ashx",
                    data: {
                        Shipmentsblitems: $("#Shipments_blitems").val(),
                        ShipmentsPer: $("#Shipments_Per").val(),
                        ShipmentsPor: $("#Shipments_Por").val(),
                        ShipmentsPeosor: $("#Shipments_Peosor").val(),
                        MemberCardId: $("#DropDownList1").val(),
                        WareHoseId: $("#DropDownList2").val(),
                        ShipmentsPrice: parseFloat(document.getElementById('Shipments_Price').innerHTML),
                        ShipmentsPricepor: $("#Shipments_Pricepor").val(),
                        ShipmentsRemark: $("#Shipments_Remark").val(),
                        xml: encodexml
                    },
                    success: function (data) {
                        if (data > 0) {
                            alert("销售订单添加成功");
                            parent.location.reload();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }
                    },
                    error: function () {
                        alert("服务器忙");
                    }
                })
            }
        }
    </script>

</body>
</html>
